package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.mapper.*;
import cn.wolfcode.trip.base.service.IMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
//消息页面显示专用
@Service
public class MessageServiceImpl  implements IMessageService {
    @Autowired
    UserCommentMapper userCommentMapper;
    @Autowired
    TravelLikeMapper travelLikeMapper;
    @Autowired
    AtMeMapper atMeMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    PrivateLetterMapper  privateLetterMapper;
    @Autowired
    SystemMessageMapper systemMessageMapper;
    @Autowired
    UserLikeMapper userLikeMapper;
    @Override
    public Message getMessage(Long id) {
        int travelCommentNum;
        int commentOfcomment;
        Integer  atmeNum;
        Integer letterNum;
        Integer systemeMessageNum;
        Integer likeNum;

        //新增的游记评论数
        try{ travelCommentNum= userCommentMapper.getNewCommentOfTravelNumByUserId(id); }catch (Exception e){ travelCommentNum=0; }
        //新增的评论的评论数
        try { commentOfcomment=userCommentMapper.getNewCommetnNUmOfCommentByUserId(id); }catch (Exception e){ commentOfcomment=0; }
        //新增的@数
        try{ atmeNum=atMeMapper.getAtMeCountofNoRead(id); }catch (Exception e){atmeNum=null;}
        //新增私信数
        try{ letterNum=privateLetterMapper.getCountofNoReader(id); }catch (Exception e){letterNum=null;}
        //新增评论数
        try{ likeNum=userLikeMapper.getCountOfNoRead(id); }catch (Exception e){likeNum=null;}

        try{ systemeMessageNum=systemMessageMapper.getCountOfNoreaderById(id); }catch (Exception e){systemeMessageNum=null;}
        Integer commentNum=travelCommentNum+commentOfcomment;
        if (commentNum==0){
            commentNum=null;
        }
        Message message=new Message();
        message.setAtMe(atmeNum);
        message.setDiscuss(commentNum);
        message.setLetter(letterNum);
        message.setSystemMessage(systemeMessageNum);
        message.setLike(likeNum);
        return message;
    }

    @Override
    public void setMessagetoNull(Long id) {
       // atMeMapper.setHasReadtoTrue(id);
       // userCommentMapper.setCommentToRead(id);
       // userCommentMapper.setTravelToReader(id);
    }

    @Override
    public List<UserComment> getCommentOfTravel(Long id) {
       List<UserComment> userComments= userCommentMapper.getTravelCommentByUserId(id);
       userComments.addAll( userCommentMapper.getCommentOfTravelCommentByUserId(id));
        return userComments;
    }

    @Override
    public List<UserComment> getCommentOfStrategyComment(Long id) {
        return userCommentMapper.getCommentOfStrategyCommentByUserId(id);
    }


    @Override
    public List<TravelLike> getTravelLikeOfNOReader(Long id) {
        return travelLikeMapper.getLikeOfNoReader(id);
    }

    @Override
    public List<AtMe> getAtMeOfNoReader(Long id,int themeType) {
       if(themeType==AtMe.THEME_STRATEGY){
         return atMeMapper.getStrategyAtMeOfNoReader(id);
       }else {
           return atMeMapper.getTravelAtMeOfNoReader(id);
       }

    }

    @Override
    public void insertAtme(String des, UserComment userComment) {
        try{
            Long userId= userMapper.getIdByUserName(des);
            AtMe atMe=new AtMe();
            atMe.setBeAtUserId(userId);
            atMe.setComment(userComment.getComment());
            atMe.setCommentTime(new Date());
            atMe.setTheme_type(userComment.getThemeType());
            atMe.setThemeId(userComment.getThemeId());
            atMe.setUserId(userComment.getUser().getId());
            atMe.setId(userComment.getId());
            atMeMapper.insert(atMe);

        }catch (Exception e){

        }
    }

    @Override
    public List<PrivateLetter> getLetterOfNoreader(Long id) {
       List<PrivateLetter>  privateLetters= privateLetterMapper.getNoReaderByUserId(id);
        return privateLetters;
    }

    @Override
    public void insertLetter(PrivateLetter privateLetter) {
        privateLetter.setPostTime(new Date());
        privateLetterMapper.insert(privateLetter);

    }

    @Override
    public void setLetterToRead(Long id) {
        privateLetterMapper.setLetterToreader(id);

    }

    @Override
    public void setAtToRead(Long id) {
        atMeMapper.setHasReadtoTrue(id);
    }

    @Override
    public void setCommnetToRead(Long id) {
        userCommentMapper.setCommentToRead(id);
        userCommentMapper.setTravelToReader(id);
    }

    @Override
    public void setSystemMessageToReader(Long id) {
        systemMessageMapper.setSystemMessageToReader(id);

    }
    @Override
    public void setUserLikeToread(Long id) {
        userLikeMapper.setToread( id);

    }

    @Override
    public List<SystemMessage> getSystemMessageOfNoreader(Long id) {
      return   systemMessageMapper.getSystemMessageById(id);
    }

    @Override
    public List<UserLike> getUserLike(Long id) {
        List<UserLike>  userLikes=  userLikeMapper.selectByUserId(id);
        return  userLikes;
    }


}
